package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.BaseListData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Groupon_rules;
import com.cskaoyan.model.promote.bo.GroupOnCreateBO;
import com.cskaoyan.model.promote.vo.GroupOnCreateVO;
import com.cskaoyan.service.PromoteService;
import com.cskaoyan.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * 团购管理 增删改查
 *
 */
@RestController
@RequestMapping("admin/groupon")
public class GroupOnController {
    @Autowired
    PromoteService promoteService;

    /**
     * 分页查询GroupOnRules表
     */
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, Integer goodsId){
        BaseListData data = promoteService.queryGroupOnRules(page, limit, sort, order, goodsId);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("listRecord")
    public BaseRespVo listRecord(Integer page, Integer limit, String sort, String order, Integer goodsId){
        BaseListData data = promoteService.queryGroupOns(page, limit, sort, order, goodsId);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Groupon_rules rules){
        int i = promoteService.updateGroupOnRules(rules);
        if(i == 0){
            return BaseRespVo.errors("商品参数不对");
        }
        return BaseRespVo.ok();
    }

    /**
     * 1。新增团购规则的逻辑
     * 2。对其中的字符串形式的date转化为java.util.date
     * @param groupOnCreateBO
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody GroupOnCreateBO groupOnCreateBO){
        String goodsId = groupOnCreateBO.getGoodsId();
        String discount = groupOnCreateBO.getDiscount();
        String discountMember = groupOnCreateBO.getDiscountMember();
        Date expireTime = groupOnCreateBO.getExpireTime();

        if(StringUtils.isEmpty(goodsId) || StringUtils.isEmpty(discount)||
        StringUtils.isEmpty(discountMember)|| expireTime == null
        || !StringUtils.isNumeric(discount) || !StringUtils.isNumeric(discountMember)){
            return BaseRespVo.errors();
        }
        GroupOnCreateVO data = promoteService.createGroupOnRules(goodsId, discount,discountMember, expireTime);
        if(data == null){
            return BaseRespVo.errors("商品参数不对");
        }
        return BaseRespVo.ok(data);

    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map<String,Object> map){
        promoteService.deleteGroupOnRules(map);
        return BaseRespVo.ok();
    }


}
